package com.boyue.service.impl;

import com.boyue.dao.AddressListDao;
import com.boyue.dao.DatabaseUtil;
import com.boyue.dao.impl.AddressListDaoImpl;
import com.boyue.entity.AddressList;
import com.boyue.service.AddressListService;
import com.boyue.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

/**
 * @auth wzy
 * @date 2023/9/1 16:22
 */
public class AddressListServiceImpl implements AddressListService {

    private AddressListDao dao = new AddressListDaoImpl();

    @Override
    public List<AddressList> list() throws SQLException {
        try {
            List<AddressList> list = dao.list();
            return list;
        }finally {
            DatabaseUtil.closeConnection();
        }
    }

    @Override
    public Integer add(AddressList addressList) throws SQLException {
        Integer result = 0;
        try {
            result = dao.add(addressList);
            // 提交事务
            DatabaseUtil.getConnection().commit();
        }catch (SQLException e) {
            e.printStackTrace();
            // 回滚事务
            DatabaseUtil.getConnection().rollback();
        } finally {
            DatabaseUtil.closeConnection();
        }
        return result;
    }

    @Override
    public PageInfo<AddressList> listPage(PageInfo pageInfo, AddressList addressList) {
        try {
            return dao.listPage(pageInfo, addressList);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeConnection();
        }
        return null;
    }

    @Override
    public Integer delete(Integer addId) throws SQLException {
        Integer result = 0;
        try {
            result = dao.delete(addId);
            DatabaseUtil.getConnection().commit();
        }catch (SQLException e) {
            e.printStackTrace();
            DatabaseUtil.getConnection().rollback();
        }finally {
            DatabaseUtil.closeConnection();
        }
        return result;
    }
}
